package com.android.place360;

import java.util.ArrayList;
import java.util.Locale;

import com.android.place360.appsettings.AppSetting;
import com.android.place360.appsettings.SharePreference_Setting;
import com.android.place360.current_location.MyLocationManager;
import com.android.place360.database.Catalog;
import com.android.place360.geocoding.FindingLocation;
import com.android.place360.google_places.PlaceRequest;
import com.android.place360.google_places.PlaceResponse;
import com.android.place360.google_places.PlaceResult;
import com.android.place360.place_detail.DetailRequest;
import com.android.place360.place_detail.DetailResponse;
import com.android.place360.place_detail.DetailResult;
import com.android.place360.place_search_result.PlaceResultActivity;
import com.android.place360.services.HttpService;
import com.android.place360.weather.ForecastConditions;
import com.android.place360.weather.WeatherRequest;
import com.android.place360.weather.WeatherResponse;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;

public class Place360 extends Activity {
	static String tag = "place360";
	final static int ACTIVITY_INDEX_SETTING = 1;
	private ArrayList<Catalog> catalogs; 
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {

		switch (requestCode) {
		case ACTIVITY_INDEX_SETTING:
			MyLocationManager.updateLocationLanguage();
			Locale locale = new Locale(SharePreference_Setting
					.getLanguage(Place360.this));
			AppSetting.ChangeLanguage(locale, Place360.this);
			break;

		default:
			break;
		}
	}

	@Override
	protected void onDestroy() {
		MyLocationManager.stopLocationListener();
		super.onDestroy();
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		// Get language setting
		Locale locale = new Locale(SharePreference_Setting.getLanguage(this));
		AppSetting.ChangeLanguage(locale, this);

		// start the location manager to get current location,
		// and update to a TextView
		TextView txtCurrentLocation = (TextView) findViewById(R.id.txtCurrentAddress);
		MyLocationManager.StartLocatonListener(getApplicationContext(),
				txtCurrentLocation);

		ImageButton btn_menu_setting = (ImageButton) findViewById(R.id.menu_setting);
		ImageButton btn_menu_search = (ImageButton) findViewById(R.id.menu_search);
		ImageButton btn_menu_add = (ImageButton) findViewById(R.id.menu_add);
		
		btn_menu_setting.setOnClickListener(menu_setting);
		btn_menu_search.setOnClickListener(menu_search);
		btn_menu_add.setOnClickListener(menu_add);
		txtCurrentLocation.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent intent = new Intent(Place360.this, FindingLocation.class);
				startActivity(intent);

			}
		});
		
		GridView gridview = (GridView) findViewById(R.id.gridview_catalog);
		
		catalogs = new ArrayList<Catalog>();
		
		catalogs.add(new Catalog("Coffee", "coffee", 0, R.drawable.c_coffee));
		
		catalogs.add(new Catalog("Shopping", "shopping", 0, R.drawable.c_shopping));
		catalogs.add(new Catalog("Restaurant", "restaurant", 0, R.drawable.c_restaurant));
		catalogs.add(new Catalog("Gas", "Gas", 0, R.drawable.c_gasstations));
		catalogs.add(new Catalog("Hospital", "hospital", 0, R.drawable.c_hospital));
		catalogs.add(new Catalog("ATM", "ATM", 0, R.drawable.c_atm));
		catalogs.add(new Catalog("Cinema", "cinema", 0, R.drawable.c_cinema));
		catalogs.add(new Catalog("Glasses", "banh mi", 0, R.drawable.c_yahoo));
		

		
		CustomAdapter adapter = new CustomAdapter(getApplicationContext(), catalogs);
		gridview.setAdapter(adapter);
		gridview.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int position,
					long arg3) {
				
				Bundle bundle = new Bundle();
				bundle.putSerializable("CATALOG", catalogs.get(position));
				Intent intent = new Intent(Place360.this, PlaceResultActivity.class);
				intent.putExtras(bundle);
				startActivity(intent);
				
			}
		});
		gridview.setOnItemLongClickListener(new OnItemLongClickListener() {

			@Override
			public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
					int arg2, long arg3) {
				Toast.makeText(getApplicationContext(), "Long Click", Toast.LENGTH_SHORT).show();
				return true;
			}
		});
		

//		// test for Google Place API
//		PlaceRequest placeRequest = new PlaceRequest();
//		placeRequest.setKey(getString(R.string.google_place_api_key));
//		placeRequest.setLanguage("vi");
//		placeRequest.setLocation("10.851188,106.755019");
//		placeRequest.setName("Coffee");
//		placeRequest.setRadius(500000);
//		placeRequest.setSensor(true);
//
//		Log.i(tag, placeRequest.getRequestString());
//		//String result = placeRequest.sendRequest();
//		// Log.v(tag, result);
//
//		// test for parse response using gson
//
//		PlaceResponse place = placeRequest.getResponse();
//		if (place != null) {
//			Log.v(tag, place.getStatus());
//			Log.v(tag, place.getHtml_attributions()[0]);
//			PlaceResult[] pResult = place.getResults();
//			Log.v(tag, String.valueOf(pResult.length));
//			for (int i = 0; i < pResult.length; i++) {
//				Log.e(tag, "_______________" + i + "________________");
//				Log.v(tag, "icon: " + pResult[i].getIcon());
//				Log.v(tag, "id: " + pResult[i].getId());
//				Log.v(tag, "name: " + pResult[i].getName());
//				Log.v(tag, "reference: " + pResult[i].getReference());
//				Log.v(tag, "vicinity: " + pResult[i].getVicinity());
//				
//			}
//			
//			//test for PlaceDetail API 
//			DetailRequest detailRequest = new DetailRequest();
//			detailRequest.setKey(getString(R.string.google_place_api_key));
//			detailRequest.setLanguage("vi");
//			detailRequest.setReference(pResult[0].getReference());
//			detailRequest.setSensor(true);
//			
//			Log.i(tag, detailRequest.getRequestString());
//			
//			DetailResponse detail = detailRequest.getResponse();
//			//Log.v(tag, "getHtml_attributions: " + detail.getHtml_attributions()[0]);
//			Log.v(tag, "getStatus: " + detail.getStatus());
//			DetailResult dResults = detail.getResult();
//			
//				Log.v(tag, "getIcon: " + dResults.getIcon());
//				Log.v(tag, "getId: " + dResults.getId());
//				Log.v(tag, "getName: " + dResults.getName());
//				Log.v(tag, "getReference: " + dResults.getReference());
//				Log.v(tag, "getVicinity: " + dResults.getVicinity());
//				Log.v(tag, "getRating: " + dResults.getRating());
//
	}
		// add catalog
		OnClickListener menu_add = new OnClickListener() {
			@Override
			public void onClick(View v) {

				//Test for calling REST web service form local host
//				Log.v(tag, "send: " + "http://10.0.2.2:8084/resources/helloWorld");
//				
//				
//				String result = HttpService.SendRequest("http://10.0.2.2:8084/resources/helloWorld");
//				
//				
//				Toast.makeText(getApplicationContext(), result, Toast.LENGTH_LONG).show();
//				Log.v(tag, "Result: " + result);
				
				WeatherRequest wr = new WeatherRequest("ho chi minh", "vi");
				WeatherResponse wre = wr.getResponse();
				if(wre!=null){
					//Log.i(tag, wre.getForecast_infomation().getCity());
					Log.i(tag, wre.getCurrent_conditions().getCondition());
					Log.i(tag, wre.getCurrent_conditions().getHumidity());
					Log.i(tag, wre.getCurrent_conditions().getIcon());
					Log.i(tag, wre.getCurrent_conditions().getTemp_c());
					Log.i(tag, wre.getCurrent_conditions().getTemp_f());
					Log.i(tag, wre.getCurrent_conditions().getWind_condition());
					for (ForecastConditions fc : wre.getForecast_conditions()) {
						Log.e(tag, "____________"+ fc.getDay_of_week() +"___________");
						Log.i(tag, fc.getCondition());
						Log.i(tag, fc.getHigh());
						Log.i(tag, fc.getLow());
						Log.i(tag, fc.getIcon());
						
					}
				}
			}
		};
	
	// Setting
	OnClickListener menu_setting = new OnClickListener() {
		@Override
		public void onClick(View v) {

			startActivityForResult(new Intent(Place360.this, AppSetting.class),
					ACTIVITY_INDEX_SETTING);
		}
	};
	
	// Setting
	OnClickListener menu_search = new OnClickListener() {
		@Override
		public void onClick(View v) {
			
			startActivity(new Intent(Place360.this, PlaceResultActivity.class));
		}
	};

}